package work20241228;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zdl
 * @date 2024/12/28
 */
class Student{
    private Integer failCount; //不及格次数
    private String name;
    private Teacher chineseTeacher; //科任老师
    private Teacher classTeacher; //班主任
    public void setIsMeritStudent(boolean isMeritStudent) {
        System.out.println(name+":三好学生");
    }

    public Student() {
    }

    public Student(Integer failCount, String name, Teacher chineseTeacher, Teacher classTeacher) {
        this.failCount = failCount;
        this.name = name;
        this.chineseTeacher = chineseTeacher;
        this.classTeacher = classTeacher;
    }

    /**
     * 获取
     * @return failCount
     */
    public Integer getFailCount() {
        return failCount;
    }

    /**
     * 设置
     * @param failCount
     */
    public void setFailCount(Integer failCount) {
        this.failCount = failCount;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return chineseTeacher
     */
    public Teacher getChineseTeacher() {
        return chineseTeacher;
    }

    /**
     * 设置
     * @param chineseTeacher
     */
    public void setChineseTeacher(Teacher chineseTeacher) {
        this.chineseTeacher = chineseTeacher;
    }

    /**
     * 获取
     * @return classTeacher
     */
    public Teacher getClassTeacher() {
        return classTeacher;
    }

    /**
     * 设置
     * @param classTeacher
     */
    public void setClassTeacher(Teacher classTeacher) {
        this.classTeacher = classTeacher;
    }

    public String toString() {
        return "Student{failCount = " + failCount + ", name = " + name + ", chineseTeacher = " + chineseTeacher + ", classTeacher = " + classTeacher + "}";
    }
}
class Teacher{
    private String teacherName;

    public Teacher() {
    }

    public Teacher(String teacherName) {
        this.teacherName = teacherName;
    }

    /**
     * 获取
     * @return teacherName
     */
    public String getTeacherName() {
        return teacherName;
    }

    /**
     * 设置
     * @param teacherName
     */
    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public String toString() {
        return "Teacher{teacherName = " + teacherName + "}";
    }
}
public class Demo1_6 {
    public static void main(String[] args) {
        /**
         * Student 类
         * 具有属性：
         * 不及格次数failCount，
         * 名字name，
         * 科任老师chineseTeacher，
         * 班主任classTeacher
         * 题目1：筛选学生不及格次数3次及3次以上的学生列表
         */
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student(3,"张三",new Teacher("安妮"),new Teacher("张远")));
        list.add(new Student(4,"李四",new Teacher("张远"),new Teacher("安妮")));
        list.add(new Student(2,"王五",new Teacher("张远"),new Teacher("张远")));
        list.add(new Student(6,"赵六",new Teacher("安妮"),new Teacher("娇娇")));
        list.add(new Student(0,"天齐",new Teacher("曹操"),new Teacher("刘备")));
        //题目1：筛选学生不及格次数3次及3次以上的学生列表
        List<Student> collect = list.stream().filter(s -> s.getFailCount() >= 3).collect(Collectors.toList());
        System.out.println(collect);

        //题目2：筛选班主任和科任老师负责的学生中有多少个不及格次数超过2次的
        Map<String, Long> collect1 = list.stream().filter(s -> s.getFailCount() > 2).collect(Collectors.groupingBy(s -> s.getChineseTeacher().getTeacherName(), Collectors.counting()));
        Map<String, Long> collect2 = list.stream().filter(s -> s.getFailCount() > 2).collect(Collectors.groupingBy(s -> s.getClassTeacher().getTeacherName(), Collectors.counting()));
        System.out.println("科任老师chineseTeacher:"+collect1);
        System.out.println("班主任classTeacher:"+collect2);
        //题目3：筛选不及格次数超过2次且班主任不是安妮和张远的
        List<Student> collect3 = list.stream()
                .filter(s -> s.getFailCount() > 2 && !(s.getClassTeacher().getTeacherName().equals("张远") || s.getClassTeacher().getTeacherName().equals("安妮")))
                .collect(Collectors.toList());
        collect3.stream().forEach(s -> System.out.println(s.getName()));


        //题目4：提取学生列表中所有学生的名字
        List<String> collect4 = list.stream()
                .map(s -> s.getName())
                .collect(Collectors.toList());
        System.out.println("学生列表中所有学生的名字:"+collect4);

        //题目5：提取不及格次数为0的学生为三好学生并给isMeritStudent 赋值
        list.stream()
                .filter(s -> s.getFailCount() == 0)
                .forEach(s -> s.setIsMeritStudent(true));

        //题目6：统计所有老师的名字
        List<String> collect5 = list.stream()
                .flatMap(s -> Stream.of(s.getChineseTeacher().getTeacherName(), s.getClassTeacher().getTeacherName()))
                .distinct()
                .collect(Collectors.toList());
        collect5.stream().forEach(s -> System.out.println(s));



    }
}
